<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="书籍名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入书籍名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="书籍类型" prop="bookType">
        <el-input
          v-model="queryParams.bookType"
          placeholder="请输入书籍类型"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <!-- =========================================== -->
    <el-row :gutter="10" style="margin-bottom: 20px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="add"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="iBorrow"
          >我借得的书</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="iLent"
          >我借出的书</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="iOffer"
          >我提供的书籍</el-button
        >
      </el-col>
    </el-row>
    <!-- 表格数据 -->
    <el-table
      :data="tableData"
      stripe
      v-loading="loading"
      border
      style="margin-bottom: 20px"
    >
      <el-table-column
        label="书名"
        prop="name"
        align="center"
      ></el-table-column>
      <el-table-column
        label="书籍种类"
        prop="bookType"
        align="center"
      ></el-table-column>
      <el-table-column
        label="出版组织"
        prop="publisher"
        align="center"
      ></el-table-column>
      <el-table-column
        label="馆藏数"
        prop="number"
        align="center"
        v-if="isshow"
      ></el-table-column>

      <el-table-column
        label="所有人"
        prop="owner"
        align="center"
        v-if="!isshow"
      ></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="addBook(scope.row)"
            v-show="isshow"
            >提供</el-button
          >
          <el-button
            size="mini"
            type="primary"
            @click="getBook(scope.row)"
            v-show="isshow"
            >借阅</el-button
          >
          <el-button
            size="mini"
            type="primary"
            @click="removeBook(scope.row)"
            v-show="removeIsshow"
            >还书</el-button
          >
          <el-button
            size="mini"
            type="primary"
            @click="hopeRemove(scope.row)"
            v-show="hopeRemoveShow"
            >希望归还</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.pageCurrent"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryParams.pageSize"
      layout="total, sizes, prev, pager, next"
      :total="total"
    ></el-pagination>

    <!-- 添加对话框 -->
    <el-dialog
      title="添加"
      :visible.sync="open"
      width="600px"
      append-to-body
      @close="close"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="书籍名" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="书籍种类" prop="bookType">
          <el-input v-model="form.bookType"></el-input>
        </el-form-item>
        <el-form-item label="书籍简介" prop="introduction">
          <el-input v-model="form.introduction"></el-input>
        </el-form-item>
        <el-form-item label="出版社" prop="publisher">
          <el-input v-model="form.publisher"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="resetForm()">重置</el-button>
      </div>
    </el-dialog>
    <!-- 借阅对话框 -->
    <el-dialog title="借阅" :visible.sync="getBookVisible">
      <el-table :data="gridData">
        <el-table-column prop="name" label="书籍名"></el-table-column>
        <el-table-column property="owner" label="持有者"></el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="getBookSub(scope.row)"
              >确定借阅</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>
<script>
import {
  selectBook,
  saveBook,
  selectUserBook,
  iBorrow,
  iLent,
  iOffer,
  offer,
  userBorrow,
  returnWord,
  hopeReturn
} from "@/api/zBook";
export default {
  data() {
    return {
      gridData: [],
      tableData: [],
      loading: false,
      // 表格条数
      total: undefined,
      queryParams: {
        pageCurrent: 1,
        pageSize: 10,
        name: undefined,
        bookType: undefined
      },
      // 是否显示弹出层
      open: false,
      viewVisible: false,
      getBookVisible: false,
      // 表单校验规则
      rules: {
        name: [{ required: true, message: "请输入书籍名", trigger: "blur" }],
        author: [{ required: true, message: "请输入作者", trigger: "blur" }],
        bookType: [
          { required: true, message: "请输入书籍类型", trigger: "blur" }
        ],
        publisher: [
          { required: true, message: "请输入出版社", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "请输入成书年份", trigger: "blur" }
        ],
        introduction: [
          { required: true, message: "请输入书籍简介", trigger: "blur" }
        ],
        dataScope: [{ required: true, message: "请选择权限" }],
        link: [{ required: true, message: "请上传书籍" }]
      },
      form: {},
      viewForm: {},
      isshow: true,
      removeIsshow: false,
      hopeRemoveShow: false
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 获取表格数据
    getList() {
      this.loading = true;
      selectBook(this.queryParams).then(res => {
        if (res.code == 200) {
          this.loading = false;
          this.tableData = res.data.records;
          this.total = res.data.total;
        }
      });
    },
    // 分页==========================================================
    handleSizeChange(pageSizeVal) {
      this.queryParams.pageSize = pageSizeVal;
      this.getList();
    },
    handleCurrentChange(pageCurrentVal) {
      this.queryParams.pageCurrent = pageCurrentVal;
      this.getList();
    },
    // 新增
    add() {
      this.open = true;
    },
    // 清空表单
    restForm() {
      this.form = {
        name: undefined,
        author: undefined,
        bookType: undefined,
        introduction: undefined,
        dataScope: undefined,
        createTime: undefined,
        publisher: undefined,
        link: undefined
      };
    },

    // 表单提交
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          saveBook(this.form)
            .then(res => {
              if (res.code == 200) {
                console.log(res);
                this.open = false;
                this.getList();
                this.$message.success(res.msg);
              }
            })
            .catch(() => {
              this.open = false;
            });
        }
      });
    },

    // 借阅
    getBook(row) {
      this.getBookVisible = true;
      selectUserBook({ wordId: row.id }).then(res => {
        if (res.code == 200) {
          this.gridData = res.data.records;
        }
      });
    },
    // 确定借阅
    getBookSub(row) {
      userBorrow({ wordUserId: row.id }).then(res => {
        if (res.code == 200) {
          this.getBookVisible = false;
          this.getList();
          this.$message.success(res.data);
        } else {
          this.$message.error(res.data);
        }
      });
    },
    // 我借到的书
    iBorrow() {
      iBorrow().then(res => {
        if (res.code == 200) {
          this.tableData = res.data.records;
          this.isshow = false;
          this.removeIsshow = true;
          this.total = res.data.total;
        }
      });
    },
    // 我借出的书
    iLent() {
      iLent().then(res => {
        if (res.code == 200) {
          this.tableData = res.data.records;
          this.isshow = false;
          this.removeIsshow = false;
          this.hopeRemoveShow = true;
          this.total = res.data.total;
        }
      });
    },
    // 我提供的书
    iOffer() {
      iOffer().then(res => {
        if (res.code == 200) {
          this.tableData = res.data.records;
          this.isshow = false;
          this.removeIsshow = false;
          this.hopeRemoveShow = false;
          this.total = res.data.total;
        }
      });
    },
    addBook(row) {
      this.$confirm("确认提供?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          offer({ wordId: row.id }).then(res => {
            if (res.code == 200) {
              this.getList();
              this.$message.success(res.data);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    // 还书
    removeBook(row) {
      returnWord({ wordUserId: row.id }).then(res => {
        if (res.code == 200) {
          this.$message.success(res.data);
          this.iBorrow();
        }
      });
    },
    // 希望归还
    hopeRemove(row) {
       hopeReturn({ wordUserId: row.id }).then((res)=>{
         if (res.code == 200) {
           this.$message.success(res.data);
         }
       })
    },

    // 重置按钮
    resetForm() {
      this.restForm();
      this.$refs.form.resetFields();
    },
    // 查看详情
    handleView(row) {
      this.viewVisible = true;
      this.viewForm = row;
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageCurrent = 1;
      this.getList();
    },
    // 重置
    resetQuery() {
      this.$refs.queryForm.resetFields();
      this.getList();
      this.isshow = true;
      this.removeIsshow = false;
      this.hopeRemoveShow = false;
    },
    close() {
      this.$refs.form.resetFields();
      this.form = {
        name: undefined,
        author: undefined,
        bookType: undefined,
        introduction: undefined,
        dataScope: undefined,
        createTime: undefined,
        publisher: undefined,
        link: undefined
      };
    }
  }
};
</script>
